package org.ilovecode.display {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.ilovecode.Utilities;
	import org.ilovecode.events.ApartmentEvent;
	import org.ilovecode.mvc.View;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class FloorBusinessSelling extends View {
		
		//private var rooms:Array;
		private var roomsWithData:Array;
		
		private var mode:String;
		
		public function FloorBusinessSelling(sellingMode:String) {
			//init();
			super();
			
			mode = sellingMode;
		}
		
		override protected function construct():void
		{
			super.construct();
			
			roomsWithData = new Array();
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			for (var i:uint = 0; i < this.numChildren; i++)
			{
				if (this.getChildAt(i) is MovieClip) {
					this.getChildAt(i).alpha = 0;
				}
			}
			
			var kg:DisplayObject = this.getChildByName("vrtic");
			setListenersForInstance(kg);
			if (mode == "kindergarten") {
				kg.alpha = 0.6;
			}
			
			var url:String =  "http://www.centar-bundek.hr/xml/searchbyfloor/9";
			Utilities.loadURL(url, loadFloorXMLCompleted);
		}
//		
//		public function init(sellingMode:String):void {
//			mode = sellingMode;
//			roomsWithData = new Array();
//			initListeners();
//			disableAll();
//			/*if (mode == "kindergarten") {
//				var kg:DisplayObject = this.getChildByName("vrtic");
//				kg.alpha = 1;
//				roomsWithData.push(kg);
//				setListenersForInstance(kg);
//			} else if (mode == "catering") {
//				var c:DisplayObject = this.getChildByName("ugostiteljstvo");
//				c.alpha = 1;
//				roomsWithData.push(c);
//				setListenersForInstance(c);
//			} else {
//				loadFloorXML();
//			}*/
//			
//			var kg:DisplayObject = this.getChildByName("vrtic");
//			setListenersForInstance(kg);
//			if (mode == "kindergarten") {
//				kg.alpha = 0.6;
//			}
//			
//			/*var c:DisplayObject = this.getChildByName("ugostiteljstvo");
//			setListenersForInstance(c);
//			if (mode == "catering") {
//				c.alpha = 0.6;
//			}*/
//			
//			loadFloorXML();
//		}
//		
//		private function disableAll():void {
//			for (var i:uint = 0; i < this.numChildren; i++)
//			{
//				if (this.getChildAt(i) is MovieClip) {
//					this.getChildAt(i).alpha = 0;
//				}
//			}
//		}
//		
//		private function initListeners():void {
//			
//		}
//		
//		private function loadFloorXML():void {
//			var url:String =  "http://www.centar-bundek.hr/xml/searchbyfloor/9";
//			Utilities.loadURL(url, loadFloorXMLCompleted);
//		}
		
		private function loadFloorXMLCompleted(event:Event):void {
			var xml:XML = XML(event.target.data);
			
			if (xml.children().length() > 0) {
				for each(var item:XML in xml.children()) {
					var id:String = item.id;
					var code:String = item.code;
					var status:String = item.state;
					var price:String = item.price;
					setRoomData(id, code, status, price);
				}
			} else {
				throw new Error("Error reading floors from database");
			}
		}
		
		private function setRoomData(id:String, code:String, status:String, price:String):void {
			var name:String = convertRoomCodeToName(code);
			var instance:DisplayObject = this.getChildByName(name);
			if (instance) {
				setListenersForInstance(instance);
				var room:FloorLayoutBaseRoom = new FloorLayoutBaseRoom(instance, id, code, status, price);
				roomsWithData.push(room);
				if (mode == "catering" && isCateringRoom(name)) {
					instance.alpha = 0.6;
				} else if (mode == "shop" && !isCateringRoom(name)) {
					instance.alpha = 0.6;
				}
			}
		}
		
		private function isCateringRoom(name:String):Boolean {
			if (name == "E_0_010" || name == "E_0_012" || name == "D_0_009") {
				return true;
			} else {
				return false;
			}
		}
		
		private function setListenersForInstance(instance:DisplayObject):void {
			if (instance != null) {
				instance.addEventListener(MouseEvent.MOUSE_OVER, roomMouseOver);
				instance.addEventListener(MouseEvent.MOUSE_OUT, roomMouseOut);
				instance.addEventListener(MouseEvent.CLICK, roomClick);
			}
		}
		
		private function roomMouseOver(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.MOUSE_OVER, event.currentTarget.name));
		}
		
		private function roomMouseOut(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.MOUSE_OUT, event.currentTarget.name));
		}
		
		private function roomClick(event:MouseEvent):void {
			this.dispatchEvent(new ApartmentEvent(ApartmentEvent.CLICK, event.currentTarget.name));
		}
		
		public function getRoomByName(name:String = ""):MovieClip {
			if (name == "vrtic") {
				return MovieClip(this.getChildByName(name));
			} else {
				for each (var room:MovieClip in roomsWithData) {
					var roomName:String = convertRoomCodeToName(FloorLayoutBaseRoom(room).code);
					if (roomName == name) {
						return room;
					}
				}
				return null;
			}
		}
		
		private function convertRoomCodeToName(code:String):String {
			var name:String = code.replace("-PP", "");
			if (name.charAt(name.length - 1) == ".") {
				name = name.slice(0, (name.length - 1));
			}
			name = name.replace(/\./gi, "_");
			return name;
		}
		
	}

}